/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.floreantpos.model.util;

/**
 *
 * @author Shane
 * 
 * 
 */

import com.floreantpos.model.Member;
import com.floreantpos.model.dao.MemberDAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CardReaderUtil {
    public  CardReaderUtil()
    {
        
    }
    public boolean validateDLstr(String input)
    {
        if (countChar(input,"?") == 3)
            return true;
        else
            return false;
    }
    public int countChar(String input,String ch)
    {
        int count=0;
        for (char c:input.toCharArray())
        {
            if (String.valueOf(c).equals(ch))
                count++;
        }
        return count;
    }
    public Member readNewMemberInput(String input)
    {
       
        //CardReaderUtil util = new CardReaderUtil();
        Member member = decodeMember(input);
        Calendar currentTime = Calendar.getInstance();
        member.setId(0);
        member.setMemberId(0);
        if (member != null)
        {
            try {
					
                Integer userWithMaxId = MemberDAO.getInstance().findMemberWithMaxId();
                member.setId(userWithMaxId + 1);
                member.setMemberId(userWithMaxId + 1);
                
                
            }
            catch (Exception ex)
            {
                int test = 5;
                test++;
                //return null;
            }
            
            member.setMemberSince(currentTime.getTime());
            
        }
        return member;
    }
//    public Member readInputClockIn(String input)
//    {
//       
//        CardReaderUtil util = new CardReaderUtil();
//        Member member = util.decodeMember(input);
//        member = MemberDAO.getInstance().findMemberByDL(member.getDlNum());
//        Calendar currentTime = Calendar.getInstance();
//        
//        member.doClockIn(currentTime);
//       
//            
//            
//        
//        return member;
//    }
    public Member readInput(String input)
    {
       try
       {
           CardReaderUtil util = new CardReaderUtil();
        Member member = util.decodeMember(input);
        member = MemberDAO.getInstance().findMemberByDL(member.getDlNum());
        return member;
       }
       catch (Exception ex)
       {
           return null;
       }
        
        
       
            
            
        
        
    }
    public Date convertStringToDate(String in){
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
 
        
        try {
            
            Date theDate = dateFormat.parse(in); 
            return theDate;
            //System.out.println("Date parsed = " + dateFormat.format(theDate));
            
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public String dateToStr(Date date){
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        
        
        if (date != null)
            return dateFormat.format(date);
        else
            return dateFormat.format(new Date());
    }
    private java.util.Date decodeDlDate(String text)
    {
        String plainText = "";
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        plainText = text.charAt(7) + text.charAt(7) + "/" +  text.charAt(4)
                + text.charAt(5) + "/" + text.charAt(0) + text.charAt(1) 
                + text.charAt(2) + text.charAt(3);
        try {
            return (dateFormat.parse(text));
        } catch (ParseException ex) {
            Logger.getLogger(CardReaderUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public Member decodeMember(String input)
    {
        Member member= new Member(); 
        try{
            String state = String.valueOf(input.charAt(1)) + String.valueOf(input.charAt(2));
            member.setState(state);
            String city = String.valueOf(input.subSequence(3, input.indexOf("^")));
            member.setCity(city);
            
            String tmp,tmp2;
            
            tmp = input.substring(input.indexOf("^")+1);
            
            tmp2 = tmp.substring(tmp.indexOf("$")+1);
            tmp = tmp.replace("$" + tmp2, "");
            String first,last,middle,address; 
            last = tmp;
            
            tmp = tmp2;
            tmp2 = tmp.substring(tmp.indexOf("$")+1);
            tmp = tmp.replace("$" + tmp2, "");
            first = tmp;
            
            tmp = tmp2;
            if (tmp.charAt(0) != '^')
            {
                if (tmp.charAt(tmp.indexOf("^")-1) == '$')
                {
                    tmp2 = tmp.substring(tmp.indexOf("$^")+2);
                    tmp = tmp.replace("$^" + tmp2, "");
                }    
                else
                {
                    tmp2 = tmp.substring(tmp.indexOf("^")+1);
                    tmp = tmp.replace("^" + tmp2, "");
                }
                middle = tmp;
                tmp = tmp2;
            }
            else
            {
                middle = "";
                tmp = tmp2;
                tmp = tmp.substring(1);
            }
                
            
      
            
            tmp2 = tmp.substring(tmp.indexOf("^")+1);
            tmp = tmp.replace("^" + tmp2, "");
            address = tmp;
            
            String dlNum,bDate,height,weight,eye,hair,zipCode;
            
            tmp = tmp2.substring(2);
            tmp2 = tmp.substring(tmp.indexOf(";") + 1,tmp.indexOf("="));
            tmp = tmp.replace(tmp2 + "=", "");
            dlNum=tmp2;
            
            tmp2 = tmp.substring(12);
            tmp = tmp.replace(tmp2,"");
            //tmp2 = tmp.substring(tmp.indexOf(";") + 1,tmp.indexOf("="));
            //tmp = tmp.replace(tmp2 + "=", "");
            bDate = tmp;
            
            tmp = tmp2.substring(4);
            zipCode = String.valueOf(tmp.charAt(0)) + String.valueOf(tmp.charAt(1))
                    + String.valueOf(tmp.charAt(2)) + String.valueOf(tmp.charAt(3))
                    + String.valueOf(tmp.charAt(4));
            
            String blank = "      C               ";
            tmp2 = zipCode + blank;
            tmp = tmp.replace(tmp2, "");
            String sex = String.valueOf(tmp.charAt(0));
            
            height = String.valueOf(tmp.charAt(1))
                    + String.valueOf(tmp.charAt(2)) + String.valueOf(tmp.charAt(3));
            weight = String.valueOf(tmp.charAt(4))
                    + String.valueOf(tmp.charAt(5)) + String.valueOf(tmp.charAt(6));
             hair = String.valueOf(tmp.charAt(7))
                    + String.valueOf(tmp.charAt(8)) + String.valueOf(tmp.charAt(9));
             eye = String.valueOf(tmp.charAt(10))
                    + String.valueOf(tmp.charAt(11)) + String.valueOf(tmp.charAt(12));
           
            
                member.setLname(last);
                member.setFname(first);
                member.setMname(middle);
                member.setAddress1(address);
                member.setCity(city);
                member.setState(state);
                member.setZipCode(zipCode);
                if ("M".equals(sex))
                    member.setSex("MALE");
                if ("F".equals(sex))
                    member.setSex("FEMALE");
                
                
                member.setBirthDate(this.decodeDlDate(bDate.substring(4)));
                member.setHeight(height);
                member.setWeight(weight);
                member.setEyeColor(eye);
                member.setHairColor(hair);
                member.setDlNum(dlNum);
                
                return member;
        }
        catch(Exception e){
            
        }
        
        
        
        return null;
    }
    
}
